#!/usr/bin/env bash

pushd `dirname $0` > /dev/null
BASE_DIR=`pwd -P`
popd > /dev/null

#############
# Functions
#############
function logging {
    echo "[INFO] $*"
}

function build_venv {
    cd ${BASE_DIR}
    if [ ! -d env ]; then
        virtualenv env
    fi
    . env/bin/activate

    pip3 install -r requirements.txt

    cd ${BASE_DIR}/mysite
}


function rebuild_db {
	logging "Clean"
	rm -rf db.sqlite3
	rm -rf account/migrations/0*.py

	logging "makemigrations" "account"
	python manage.py makemigrations account

	logging "migrate"
	python manage.py migrate

	logging "initdb.py"
	python initdb.py
}

function launch_webapp {
    #python "manage.py" "runserver"
    python manage.py runserver 192.168.3.235:8000  #获得 $ ifconfig

}

#############
# Main
#############
OPT_ENV_FORCE=$1
build_venv

if [ "${OPT_ENV_FORCE}x" == "-ix" ];then
    rebuild_db
fi

launch_webapp
